<?php

if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

class Home extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
	}

	//首页
	public function index() {
 		//$this->output->cache(60*24);
		//$this->template->caching=true;
		//$this->template->cache_lifetime = 604800;
		//$this->load->service("login_service");
		/*if($this->login_service->is_login())
		{
			$is_login = TRUE;
		}
		else
		{
			$is_login = FALSE;
		}
		$this->template->assign("is_login",$is_login);*/
		$this->load->library('user_agent');
		if($this->agent->is_mobile()){
			//redirect("/m");
			$this->m_index();
			return;
		}
		$cache_key = "home-index";
		$this->load->driver('cache',config_item("view_cache_handle"));
		if (!$cache_data = $this->cache->get($cache_key))
		{
			$seo_obj = (object)[
				'title' 		=> "易控 - 岂止阅后即焚！？",
				'keywords' 		=> "易控,文件分享，数字内容，阅后即焚，外发控制，文件加密，安全分享，文件保护 ， 外发文件安全，信息安全，隐私保护，商用信息保护，专治盗图党，安全交易，分享保密，保护智慧资产，信息保护",
				'description'	=> "易控安全分享平台是一款以一流的数据安全防护技术打造的创新型数字内容安全分享平台，可有效保护企业或个人的商业合同、课件、设计稿、提案、视频等智慧资产不受盗版侵扰。不同于市面上的同类产品，易控提供“七重防护”数据安全保护方案，并支持全平台同步操作，兼顾了安全性和体验性。"
			];

			$this->template->assign("site", $seo_obj);
			$this->template->assign("current_nav",$this->router->method);
			$cache_data = $this->template->view('v1/index.html','',TRUE);
			$this->cache->save($cache_key,$cache_data,config_item("view_cache_time"));
		}
		echo $cache_data;

	}

	//手机版首页
	public function m_index() {
		$cache_key = "home-index-mobile";
		$this->load->driver('cache',config_item("view_cache_handle"));
		if (!$cache_data = $this->cache->get($cache_key)) {
			$cache_data = $this->template->view('mv1/index.html','',TRUE);
			$this->cache->save($cache_key,$cache_data,config_item("view_cache_time"));
		}
		echo $cache_data;
	}

	//手机版行业
	public function m_industry($style="") {
		$cache_key = "home-industry-mobile";

		!in_array($style, array('design', 'outsourcing', 'education')) && $style = 'outsourcing';
		$tmp_name = sprintf('mv1/%s.html', $style);
		$cache_key .= "-".$style;

		$this->load->driver('cache',config_item("view_cache_handle"));
		if (!$cache_data = $this->cache->get($cache_key))
		{
			$cache_data = $this->template->view($tmp_name, '',TRUE);
			$this->cache->save($cache_key,$cache_data,config_item("view_cache_time"));
		}
		echo $cache_data;
	}

	//产品
	public function product(){
		$cache_key = "home-product";
		$this->load->driver('cache',config_item("view_cache_handle"));
		if (!$cache_data = $this->cache->get($cache_key))
		{
			$seo_obj = (object)[
				'title' 		=> "易控 - 产品",
				'keywords' 		=> "易控,文件加密,文件受控,外发文件,阅后即焚,云盘,免费云盘",
				'description'	=> "易控是基于云端控制，以APP，Web，PC客户端落地的一款受控外发文件安全的革命性产品。其着力于打造企业与个人用户间随时随地控制、跨平台落地的信息安全分享生态圈。"
			];

			$this->template->assign("site", $seo_obj);
			$this->template->assign("current_nav",$this->router->method);
			$cache_data = $this->template->view('v1/product.html','',TRUE);
			$this->cache->save($cache_key,$cache_data,config_item("view_cache_time"));
		}
		echo $cache_data;
	}

	//行业
	public function industry($style=""){
		$cache_key = "home-industry";
		$style_name = "";
		$industry_title = "";

		if (! empty($style) &&
			in_array($style, array('design', 'outsourcing', 'education'))) {

			$cache_key .= "-".$style;
			$style_name = "_".$style;

			$title_arr = array(
				'design' 		=> "设计行业",
				'outsourcing' 	=> "众包行业",
				'education' 	=> "在线教育行业",
			);

			$industry_title = " - ".$title_arr[$style];
		}

		$tmp_name = sprintf('v1/industry%s.html', $style_name);

		$this->load->driver('cache',config_item("view_cache_handle"));
		if (!$cache_data = $this->cache->get($cache_key))
		{

			$seo_obj = (object)[
				'title' 		=> "易控 - 行业".$industry_title,
				'keywords' 		=> "易控,文件加密,文件受控,外发文件,阅后即焚,云盘,免费云盘",
				'description'	=> "易控是基于云端控制，以APP，Web，PC客户端落地的一款受控外发文件安全的革命性产品。其着力于打造企业与个人用户间随时随地控制、跨平台落地的信息安全分享生态圈。"
			];

			$this->template->assign("site", $seo_obj);
			$this->template->assign("current_nav",$this->router->method);
			$cache_data = $this->template->view($tmp_name,'',TRUE);
			$this->cache->save($cache_key,$cache_data,config_item("view_cache_time"));
		}
		echo $cache_data;
	}

	//下载
	public function download() {

		/*
		$this->load->library('user_agent');
		if($this->agent->is_mobile()){
			redirect("/m/index.html#index/mobile");
		} */

		$cache_key = "home-download";
		$this->load->driver('cache',config_item("view_cache_handle"));
		if (!$cache_data = $this->cache->get($cache_key))
		{
			/*
			$this->template->assign("ios_version",$this->config->item("ios_version"));
			$this->template->assign("android_version",$this->config->item("android_version"));
			$this->template->assign("reader_version",$this->config->item("reader_version"));
			$this->template->assign("pc_version",$this->config->item("pc_version"));
			//$this->template->assign("ios_download",$this->config->item("ios_download"));*/

			$seo_obj = (object)[
				'title' 		=> "易控 - 下载",
				'keywords' 		=> "易控,文件加密,文件受控,外发文件,阅后即焚,云盘,免费云盘",
				'description'	=> "易控是基于云端控制，以APP，Web，PC客户端落地的一款受控外发文件安全的革命性产品。其着力于打造企业与个人用户间随时随地控制、跨平台落地的信息安全分享生态圈。"
			];

			$this->template->assign("site", $seo_obj);
			$this->template->assign("current_nav",$this->router->method);
			//$cache_data = $this->template->view('home/download.tpl','',TRUE);
			$cache_data = $this->template->view('v1/download.html','',TRUE);
			$this->cache->save($cache_key,$cache_data,config_item("view_cache_time"));
		}
		echo $cache_data;
	}

	//开放平台
	public function open() {
		redirect("https://open.ekong366.com");
	}

	public function err() {
		$this->template->view('v1/404.html');
	}

	//关于我们
	public function about(){
		$cache_key = "home-about";
		$this->load->driver('cache',config_item("view_cache_handle"));
		if (!$cache_data = $this->cache->get($cache_key))
		{
			$seo_obj = (object)[
				'title' 		=> "易控 - 关于我们",
				'keywords' 		=> "易控,文件加密,文件受控,外发文件,阅后即焚,云盘,免费云盘",
				'description'	=> "易控是基于云端控制，以APP，Web，PC客户端落地的一款受控外发文件安全的革命性产品。其着力于打造企业与个人用户间随时随地控制、跨平台落地的信息安全分享生态圈。"
			];

			$this->template->assign("site", $seo_obj);
			$this->template->assign("current_nav",$this->router->method);
			//$cache_data = $this->template->view('home/about.tpl','',TRUE);
			$cache_data = $this->template->view('v1/about.html','',TRUE);
			$this->cache->save($cache_key,$cache_data,config_item("view_cache_time"));
		}
		echo $cache_data;
	}

	//联系我们
	public function contact(){
		$cache_key = "home-contact";
		$this->load->driver('cache',config_item("view_cache_handle"));
		if (!$cache_data = $this->cache->get($cache_key))
		{
			$seo_obj = (object)[
				'title' 		=> "易控 - 联系我们",
				'keywords' 		=> "易控,文件加密,文件受控,外发文件,阅后即焚,云盘,免费云盘",
				'description'	=> "易控是基于云端控制，以APP，Web，PC客户端落地的一款受控外发文件安全的革命性产品。其着力于打造企业与个人用户间随时随地控制、跨平台落地的信息安全分享生态圈。"
			];

			$this->template->assign("site", $seo_obj);
			$this->template->assign("current_nav",$this->router->method);
			$cache_data = $this->template->view('v1/contact.html','',TRUE);
			$this->cache->save($cache_key,$cache_data,config_item("view_cache_time"));
		}
		echo $cache_data;
	}

	//地图
	public function map(){
		$seo_obj = (object)[
			'title' 		=> "易控 - 公司地图",
			'keywords' 		=> "易控,文件加密,文件受控,外发文件,阅后即焚,云盘,免费云盘",
			'description'	=> "易控是基于云端控制，以APP，Web，PC客户端落地的一款受控外发文件安全的革命性产品。其着力于打造企业与个人用户间随时随地控制、跨平台落地的信息安全分享生态圈。"
		];

		$this->template->assign("site", $seo_obj);
		$this->template->assign("current_nav",$this->router->method);
		$this->template->view('v1/map.html');
	}

	//wap 页面
	public function app()
	{
		redirect("/");
		return;
		$cache_key = "home-app";
		$this->load->driver('cache',config_item("view_cache_handle"));
		if (!$cache_data = $this->cache->get($cache_key))
		{
			$this->template->assign("android_download",$this->config->item("android_download"));
			$this->template->assign("current_nav",$this->router->method);
			$cache_data = $this->template->view('home/app.tpl','',TRUE);
			$this->cache->save($cache_key,$cache_data,config_item("view_cache_time"));
		}
		echo $cache_data;
	}
	
	public function is_login(){
		$this->load->service("login_service");
		//登录判断
		$isLogin = 0;//0未登录，1正常登录，2试用
		$uname = '';
		$user = $this->login_service->get_user_info();
		if(!empty($user)/*&& !$this->login_service->is_tryout()*/)
		{
			$uname = $this->login_service->get_show_name($user);
			$isLogin =1;
		}
		/*$json = array(
				'state'    => $isLogin,
				'username' => $email
				);
		echo json_encode($json);*/
		$data = array(
			'state'    => $isLogin,
			'username' => $uname
		);
		Common::ajax_return(0,NULL,$data,FALSE);
	}


    public function no_mobile()
    {
		$cache_key = "home-no-mobile";
		$this->load->driver('cache',config_item("view_cache_handle"));
		if (!$cache_data = $this->cache->get($cache_key))
		{
			$cache_data = $this->template->view('no_mobile.tpl','',TRUE);
			$this->cache->save($cache_key,$cache_data,config_item("view_cache_time"));
		}
		echo $cache_data;

    }

	/**
	 * 版本修改记录
	 */
	public function version() {
		$this->load->library('user_agent');
		if ($this->agent->is_robot()) {
			redirect("/");
		}

		$changelog = file_get_contents("changelog.html");
		echo "最新版本号:v".UNCLONE_VERSION." 发布时间:".date("Y-m-d H:i:s",filemtime("index.php"))."<br />".preg_replace('/\n\r/', '<br />',$changelog);
	}

	/**
	 * 移动端下载
	 */
	public function download_mobile() {
		$this->load->library('user_agent');

		//蜘蛛
		if ($this->agent->is_robot()) {
			return;
		}

		//apple系列
		if ($this->agent->is_mobile('iphone') ||
			$this->agent->is_mobile('ipod') ||
			$this->agent->is_mobile('ipad')) {

			return $this->download_ios();

		//mobile
		} elseif ($this->agent->is_mobile()) {
			//agent
			$full_agent = $this->agent->agent_string();
			//判断是否为android
			if (strpos(strtolower($full_agent), 'android')) {
				if ($this->is_weixin()) {
					redirect("/m/download.html#downloadindex/index");
					return;
				}
				return $this->download_android();
			}
		} else {
			redirect("/");
		}

		//其它为wp/bbm等非ios和android平台
	}

	/**
	 * iOS下载
	 */
	public function download_ios(){
		$this->setDownloadStat("ios");
		//return $this->coming_soon();
		$url = $this->config->item("ios_download");
		redirect($url);
	}

	/**
	 * Android下载
	 */
	public function download_android(){
		$this->setDownloadStat("android");
		//return $this->coming_soon();
		$last_version = $this->config->item("android_version");
		$url = "/download/ekong_v".$last_version.".apk";
		redirect($url);
	}

	/**
	 * PC下载
	 */
	public function download_client() {
		$this->setDownloadStat("pc");
		//return $this->coming_soon();
		$last_version = $this->config->item("pc_version");
		$url = "/download/ekong_v".$last_version.".exe";
		redirect($url);
	}

	/**
	 * 阅读器下载
	 */
	public function download_reader(){
		$this->setDownloadStat("reader");
		//return $this->coming_soon();
		$last_version = $this->config->item("reader_download");
		//$url = "/download/pc_".$last_version.".exe";
		redirect($last_version);
	}
	
	/**
	 * 下载统计
	 * @return array
	 */
	public function stat() {
		//header("access-control-allow-origin: *");

		$this->load->library('user_agent');
		if ($this->agent->is_robot()) {
			redirect("/");
		}
		
		$redis = new Redis();
		$connect = $redis->connect($this->config->item("host","redis"),$this->config->item("port","redis"),$this->config->item("timeout","redis"));

		if(! $connect) {
			return Common::ajax_return(1019);
		}

		$redis_hkey = "__DOWNLOAD_STAT__";
		$total_arr = $redis->hGetAll($redis_hkey);

		$android_stat = empty($total_arr['android']) ? 0 : (int)$total_arr['android'];
		$ios_stat     = empty($total_arr['ios'])     ? 0 : (int)$total_arr['ios'];
		$reader_stat  = empty($total_arr['reader'])  ? 0 : (int)$total_arr['reader'];
		$pc_stat      = empty($total_arr['pc'])      ? 0 : (int)$total_arr['pc'];

		$data = array(
			'mobile' => $this->stat_formula($android_stat + $ios_stat),
			'reader' => $this->stat_formula($reader_stat),
			'pc'	 => $this->stat_formula($pc_stat)
		);

		return Common::ajax_return(0, null, array("data" => $data));
	}

	/**
	 * 统计公式
	 */
	private function stat_formula($num=0) {
		return 1e6 + $num * 2;
	}

	/**
	 * 更新下载统计
	 * @param $client
	 */
	private function setDownloadStat($client) {
		if (empty($client) || !in_array($client, array("android", "ios", "pc", "reader"))) {
			return Common::ajax_return(1003);
		}

		$redis = new Redis();
		$connect = $redis->connect($this->config->item("host","redis"),$this->config->item("port","redis"),$this->config->item("timeout","redis"));

		if(! $connect) {
			return Common::ajax_return(1019);
		}

		$redis_hkey = "__DOWNLOAD_STAT__";

		//自增1
		$set = $redis->hIncrBy($redis_hkey, $client, 1);

		if(! $set) {
			return Common::ajax_return(1019);
		}
	}

	/**
	 * 虚假的文件、用户及查阅统计数
	 * @return array
	 */
	public function protect_stat() {
		header('Access-Control-Allow-Origin: *');

		$redis = new Redis();
		$connect = $redis->connect($this->config->item("host","redis"),$this->config->item("port","redis"),$this->config->item("timeout","redis"));

		if (! $connect) {
			return Common::ajax_return(1019);
		}

		$protect_files_hkey = "__PROTECT_STAT__";

		$ts = (int)$this->input->get('_ts', true);
		$interval = 60 * 1000;

		$get_ts = $redis->hGet($protect_files_hkey, '_ts');

		//按时间间隔更新，防止多用户，多端触发过多
		if ($ts - $interval >= $get_ts) {
			$view_total = substr(uniqid(rand(), true), -3, 3);
			$protect_total = substr(uniqid(rand(), true), -2, 2);
			$user_total = substr(uniqid(rand(), true), -1, 1);

			$set1 = $redis->hIncrBy($protect_files_hkey, 'view', $view_total);
			if(! $set1) {
				return Common::ajax_return(1019);
			}

			$set2 = $redis->hIncrBy($protect_files_hkey, 'protect', $protect_total);
			if(! $set2) {
				return Common::ajax_return(1019);
			}

			$set3 = $redis->hIncrBy($protect_files_hkey, 'user', $user_total);
			if(! $set3) {
				return Common::ajax_return(1019);
			}

			list($s1,$s2) = explode(' ',microtime());
			$now_ts = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);

			$set_ts = $redis->hSet($protect_files_hkey, '_ts', $now_ts);
			if($set_ts) {
				return Common::ajax_return(1019);
			}
		}

		$data = $redis->hGetAll($protect_files_hkey);

		return Common::ajax_return(0, null, array("data" => $data), FALSE);

	}

	private function coming_soon() {
		$data = array(
			"title"=>"产品正在维护升级……",
			"type" =>"success",
			"msg"  =>"敬请期待！",
			"url"  =>"/",
			"time" =>5
		);
		$this->template->view("common/message.tpl",$data);
	}

	/**
	 * 是否微信打开
	 * @return bool
	 */
	public function is_weixin(){
		if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
			return true;
		}
		return false;
	}
	
}
